#include <iostream>
#include <set>
#include <algorithm>
using namespace std;

int main()
{
    int n1,n2;
    cin>>n1>>n2;
    set<int>s1;
    set<int>s2;
    for (int i=0;i<n1;i++)
    {
        int p;
        cin>>p;
        s1.insert(p);
    }
    for (int i=0;i<n2;i++)
    {
        int p;
        cin>>p;
        s2.insert(p);
    }
    set<int>res1;
    set<int>res2;
    set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(res1,res1.begin()));
    set_union(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(res2,res2.begin()));
    cout<<res1.size();
    for (int i:res1)
        cout<<" "<<i;
    cout<<endl;
    cout<<res2.size();
    for (int i:res2)
        cout<<" "<<i;
}